package com.satyam.smooth.hibernateutils;

import java.io.File;
import java.io.IOException;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.hbm2ddl.SchemaUpdate;

public class HibernateSchemaUtils {
	public static void updateSchema(boolean ddlToConsole) throws ClassNotFoundException, IOException{
		Configuration cfg = new Configuration().configure();
		SchemaUpdate schemaUpdate = new SchemaUpdate(cfg);
		schemaUpdate.execute(ddlToConsole, true);
	}
	
	public static void exportSchema(Dialect dialect ) throws IOException {
		Configuration cfg = new Configuration().configure();
		cfg.setProperty("hibernate.dialect", dialect.getDialectClass());
		SchemaExport schemaExport = new SchemaExport(cfg);
		schemaExport.setDelimiter(";");
		schemaExport.setOutputFile(new File(".").getCanonicalPath() + "\\src\\main\\java\\resources\\schema.sql");
		schemaExport.create(false,true);
	}
	
	public static enum Dialect {
        ORACLE("org.hibernate.dialect.Oracle10gDialect"),
        MYSQL("org.hibernate.dialect.MySQLDialect"),
        HSQL("org.hibernate.dialect.HSQLDialect"),
        H2("org.hibernate.dialect.H2Dialect");
 
        private String dialectClass;
 
        private Dialect(String dialectClass) {
            this.dialectClass = dialectClass;
        }
 
        public String getDialectClass() {
            return dialectClass;
        }
    }
}
